using Updater.Common.Database.Properties;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using System;
using log4net;

namespace Updater.Common.Database
{
    class SqlScripting
    {
        public static bool BuildStringAndInitConnection(string dataSource)
        {
            return InitConnection(string.Format(_connectionStringFormat, dataSource, "master", "Integrated Security=True"));
        }

        public static bool BuildStringAndInitConnection(string dataSource, string user, string password)
        {
            return InitConnection(string.Format(_connectionStringFormat, dataSource, "master", "UID="+ user +";PWD="+ password));
        }

        private void CheckConnection()
        {
            bool r = false;

           // r = SqlScripting.BuildStringAndInitConnection(txtSqlServerInstance.Text);
         
           // r = SqlScripting.BuildStringAndInitConnection( txtSqlServerInstance.Text, txtUsername.Text, txtPassword.Text);

            
            
         //   MessageBox.Show(this, "Invalid Sql Server 2005 Connection Parameters provided", "Can't connect");
        }

        private const string DB_LOGIN = "wseuser"; // TODO: fetch from K:L:O:N:K configuration settings
        private const string HIV_DB_NAME = "WSEDeployment"; // TODO: fetch from K:L:O:N:K configuration settings
        private static string _connectionStringFormat = "Data Source={0};Initial Catalog={1};{2}"; // TODO: fetch from K:L:O:N:K configuration settings

        private static string _connectionString = null;
        public static string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }
        
        public static bool InitConnection(string connectionString)
        {
            string query = "SELECT TOP 1 * FROM sys.objects";

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = query;
            cmd.CommandType = CommandType.Text;
            cmd.Connection = new SqlConnection(connectionString);

            try
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();

                ConnectionString = connectionString;
                return true;
            }
            catch (Exception ex)
            {
                ConnectionString = null;
                return false;
            }
        }

        public static string InstallDatabase()
        {
            string[] SqlLine;
            Regex regex = new Regex("^GO", RegexOptions.IgnoreCase | RegexOptions.Multiline);

            string password = new Random().Next(1000000000).ToString();
            string txtSQL = string.Format(Resource.SqlScriptInstall, DB_LOGIN, password, HIV_DB_NAME);

            SqlLine = regex.Split(txtSQL);

            SqlCommand cmd = null;
            try
            {
                cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.Connection = new SqlConnection(ConnectionString);
                cmd.Connection.Open();

                foreach (string line in SqlLine)
                {
                    if (line.Length > 0)
                    {
                        cmd.CommandText = line;
                        cmd.ExecuteNonQuery();
                    }
                }

                string[] connStringParts = ConnectionString.Split(';');
                string databaseConnectionString = string.Format("{0};Initial Catalog={3};UID={1};PWD={2}",connStringParts[0], DB_LOGIN, password, HIV_DB_NAME);
                return databaseConnectionString;
            }
            finally
            {
                cmd.Connection.Close();
            }
        }

        public static void UninstallDatabase()
        {
            string query = string.Format(Resource.SqlScriptUninstall, DB_LOGIN, HIV_DB_NAME);

            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = query;
                cmd.CommandType = CommandType.Text;
                cmd.Connection = new SqlConnection(ConnectionString);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                // DB Already dropped
                Console.Write(ex);
            }
        }
    }
}